﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Principal;
using System.Web;
using System.Web.Caching;

namespace MB.TheBeerHouse.BLL
{
   public abstract class BaseEntity
   {      
      protected static Cache Cache
      {
         get { return HttpContext.Current.Cache; }
      }
      
      protected static IPrincipal CurrentUser
      {
         get { return HttpContext.Current.User; }
      }

      protected static string CurrentUserName
      {
         get
         {
            if (CurrentUser.Identity.IsAuthenticated)
               return CurrentUser.Identity.Name;
            else
               return String.Empty;
         }
      }

      protected static string CurrentUserIP
      {
         get { return HttpContext.Current.Request.UserHostAddress; }
      }

      protected static string ConvertNullToEmptyString(string input)
      {
         return (input == null ? "" : input);
      }

      protected static void PurgeCacheItems(string prefix)
      {
         prefix = prefix.ToLower();
         List<string> itemsToRemove = new List<string>();

         IDictionaryEnumerator enumerator = Cache.GetEnumerator();
         while (enumerator.MoveNext())
         {
            if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
               itemsToRemove.Add(enumerator.Key.ToString());
         }

         foreach (string itemToRemove in itemsToRemove)
            Cache.Remove(itemToRemove);
      }
   } 
}
